package org.refresh.core.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.refresh.injectors.xml.InvalidInjectionException;
import org.refresh.util.ParsingUtil;

/*******************************************************************************
 * Copyright (c) 2005 Jules White. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public class SetVariableHandler implements ConfigDirectiveHandler {

	
	public class InvalidVariableDeclarationException extends RuntimeException {
		public InvalidVariableDeclarationException(String msg){
			super(msg);
		}
	}
	
	public String[] resolve(String context, String var){
		int first = var.indexOf(".");
		String[] resolved = new String[2];
		if(first > 0){
			resolved[0] = var.substring(0,first);
			resolved[1] = var.substring(first+1);
		}
		else{
			resolved[0] = context;
			resolved[1] = var;
		}
		return resolved;
	}
	
	public void handle(RefreshProblem problem, String context, String directive, String argstr) {
		String[] parts = argstr.split("=");
		if(parts.length != 2)
			throw new InvalidVariableDeclarationException("A variable declaration must use the form \"VAR = VALUE\" and the declaration \""+argstr+"\" in context \""+context+"\" does not.");
		String var = parts[0];
		String val = parts[1];
		
		Map<String,Object> ivals = problem.getInjectionValues(context);
		if(ivals == null){
			ivals = new HashMap<String, Object>();
			problem.setInjectionValues(context, ivals);
		}
		Object eval = ivals.get(var);
		if(eval != null && !(eval instanceof List)){
			throw new InvalidInjectionException("The variable \""+var+"\" in context \""+context+"\" is not a list and and already has a value set.");
		}
		
		Object dval = val;
		if(val.startsWith("[")){
			dval = ParsingUtil.parseList(val);
		}
		else if(val.startsWith("{")){
			dval = ParsingUtil.parseMap(val);
		}
		if(eval != null){
			List lval = (List)eval;
			lval.add(dval);
			dval = lval;
		}
		
		String[] resolved = resolve(context, var);
		ivals.put(resolved[0]+"."+resolved[1],dval);
	}

}
